#include "head.h"
/* Usage: */

void *thread_main(void *p){
    status_t *status = (status_t*)p;
    pthread_mutex_lock(&status->db_lock);
    MYSQL *th_sql = getsql();
    pthread_mutex_unlock(&status->db_lock);
    while(1){
        printf("thread start!\n");
        pthread_mutex_lock(&status->lock);

        while(isEmpty(status->commend_q) && status->exit_flag==false){
            pthread_cond_wait(&status->cond, &status->lock);
        }
        if(status->exit_flag){
            printf("exit!!!!\n");
            pthread_mutex_unlock(&status->lock);
            mysql_close(th_sql);
            pthread_exit(NULL);
        }

        client_status* user = pop(status->commend_q);
        user->sql = th_sql;
        pthread_mutex_unlock(&status->lock);
        printf("与%d客户端开始工作！\n", user->net_fd);
        if(user->msg->mode==3){
            my_puts(*user->msg, user, user->net_fd);
        } else if(user->msg->mode==4){
            my_gets(*user->msg, user, user->net_fd);
        }
        //work(user, status);
        printf("与%d客户端工作完成！\n", user->net_fd);
        free(user->msg);
        close(user->net_fd);
    }

    return NULL;
}
